//
//  XNBaseRequest.m
//  XINNingOA
//
//  Created by samuel on 2021/7/19.
//

#import "XNBaseRequest.h"
#import "XNLoginViewController.h"
#import "XNMainViewController.h"
#import "SVProgressHUD.h"
#import "XNHud.h"

@implementation XNBaseRequest


- (BOOL)success
{
    NSDictionary *response = [self responseJSONObject];
    if ([response[@"isSucceed"] boolValue]) {

        NSString *token = response[@"loginToken"];
        if (token.length > 0 && ![token isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"loginToken"]]) {
            NSLog(@"------不是一个账号");
            [SVProgressHUD dismiss];
            [XNHud showToastWithText:@"账号已在另一台设备上登录"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"account"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginToken"];
            
            UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
            if ([vc isKindOfClass:[UINavigationController class]]) {
                UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                XNMainViewController *rootVC = nav.viewControllers.firstObject;
                [rootVC.timer invalidate];
                rootVC.timer = nil;
            }
            [UIApplication sharedApplication].keyWindow.rootViewController = [[XNLoginViewController alloc] init];
        }
        
        
        return true;
    }else{
        return false;
    }
}

- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary
{
    return @{
        @"Account":[[NSUserDefaults standardUserDefaults] objectForKey:@"account"],
    };
}

@end
